<?php

namespace App\model;

use support\Model;

class ChatHistory extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'chat_history';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    protected $guarded = [];
    protected $dateFormat = 'U';

    // 定义一个访问器，用于格式化输出
    public function getCreatedAtAttribute($value)
    {
        return date('Y-m-d H:i:s', strtotime($value));
    }

    /**
     * @param string $token
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author xqiaochina@163.com
     * 根据用户id查询用户信息
     */
    public function getLastedHistory($type, $total = 20)
    {
        $lists = $this->where('type', $type)->orderBy('id', 'desc')->limit($total)->get();
        return $lists ? $lists->toArray() : [];
    }

    public function getLastedAllHistory($uuid, $total = 20)
    {
        $lists = $this->where('uuid', $uuid)->orderBy('id', 'desc')->take($total)->get();
        return $lists ? $lists->toArray() : [];
    }


    public function fmtLastedHistory($data)
    {
        $temp_data = [];
        foreach ($data as $key => $datum) {
            $temp_data[$key + 1] = $datum;
        }
        return $temp_data;
    }


}
